package com.woniuxy.service.impl;

import com.woniuxy.dao.RbacManagerDao;
import com.woniuxy.dao.RbacMenuDao;
import com.woniuxy.entity.RbacMenu;
import com.woniuxy.entity.ResponseEntity;
import com.woniuxy.service.RbacMenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;


@Service
public class RbacMenuServiceImpl implements RbacMenuService {
    @Autowired
    RbacMenuDao rbacMenuDao;

    @Autowired
    RbacManagerDao rbacManagerDao;

    @Override
    public ResponseEntity findAllMenu() {
        List<RbacMenu> rbacMenus = rbacMenuDao.findAll();
        rbacMenus.forEach(System.out::println);
        return new ResponseEntity(200, "请求成功！", rbacMenus);
    }

    @Override
    public ResponseEntity findMenuById(int id) {
        Integer[] menuId = rbacMenuDao.findMenuIds(id);
        List<RbacMenu> rbacMenus = rbacMenuDao.findByIds(menuId);
        return new ResponseEntity(200, "请求成功！", rbacMenus);
    }

    @Override
    public ResponseEntity editOne(RbacMenu rbacMenu) {
        int i = rbacMenuDao.editOne(rbacMenu);
        if (i > 0) {
            return new ResponseEntity(200, "成功修改");
        } else {
            return new ResponseEntity(500, "修改失败");
        }
    }

    @Override
    public ResponseEntity updateMenu(int id, Integer fid) {
        if (fid == 0) {
            fid = null;
        }
        int i = rbacMenuDao.updateMenu(id, fid);
        if (i > 0) {
            return new ResponseEntity(200, "成功修改");
        } else {
            return new ResponseEntity(500, "修改失败");
        }
    }


    @Override
    public ResponseEntity addRoleMenu(int rid, int[] ids) {
        if (ids.length == 0) {
            return ResponseEntity.SUCCESS;
        } else {
            for (int i = 0; i < ids.length; i++) {
                int j = rbacMenuDao.addRoleMenu(rid, ids[i]);
            }
            return ResponseEntity.SUCCESS;
        }
    }

    @Override
    public ResponseEntity deleteRoleMenu(int rid, int[] ids) {
        if (ids.length == 0) {
            return ResponseEntity.SUCCESS;
        } else {
            for (int i = 0; i < ids.length; i++) {
                int j = rbacMenuDao.deleteRoleMenu(rid, ids[i]);
            }
            return ResponseEntity.SUCCESS;
        }
    }

    @Override
    public ResponseEntity getRoleMenuIds(int id) {
        Integer[] ids = rbacMenuDao.findMenuIds(id);
        return new ResponseEntity(200, "成功", ids);
    }

    @Override
    public ResponseEntity findMenuIdsByRoleId(int rid) {
        Integer[] ids = rbacMenuDao.findMenuIdsByRoleId(rid);
        return new ResponseEntity(200, "成功", ids);
    }
}